/**
 * 
 */
package ac.technion.StreamMania;

import java.util.HashMap;
import java.util.Map;

//import ac.technion.StreamMania.R;
import ac.technion.StreamMania.application.state.Person;
import ac.technion.StreamMania.application.uielements.iface.IParticipantsUIElement;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * The implementation class of IParticipantsUIElement.
 * The element is initialized with ViewGroup to hold the list of participants.
 * First participant added there will always be self - own name.
 * When new participant is added his name is displayed in bold.
 * When a participant disconnects his name becomes strikethrough.
 * When a participants leaves his name is removed from the container ViewGroups.
 * When a participant is a sender, the writing "SENDER" will be added to the name.
 * 
 * @author arnony
 * 
 */
public class ParticipantsUIElement implements IParticipantsUIElement
{
	private final Handler m_uiHandler;
	private final Map<Person, TextView> m_mpOtherParticipants = new HashMap<Person, TextView>();
	private final ViewGroup m_participantsView;
	private final Context m_context;
	
	public ParticipantsUIElement(Handler uiHandler, ViewGroup participantsView, String selfName, Context context, boolean isSender)
	{
		m_uiHandler = uiHandler;
		m_participantsView = participantsView;
		m_context = context;
		String isSenderStr;
		isSenderStr = (isSender) ? " (SENDER) " : "";
		TextView selfTextView = buildTextView(selfName + " (self)" + isSenderStr);
		m_participantsView.addView(selfTextView);
	}
	
	/**
	 * The function builds a text according to the given content.
	 * @param content - The message itself.
	 * @return the result TextView object.
	 */
	private TextView buildTextView(String content)
	{
		TextView textView = new TextView(m_context);
		textView.setText(content);
		textView.setPadding(5, 0, 0, 0);
		return textView;
	}
	
	/**
	 * The function adds a new person to the participants list.
	 * @param newPerson is the person to be added.
	 */
	public void personAdded(final Person newPerson)
	{
		m_uiHandler.post(new Runnable()
			{
				public void run()
				{
					String senderString = (newPerson.getIsSender() == true) ? " (SENDER)" : "";
					final TextView personTextView = buildTextView(newPerson.getName() + senderString);
					m_mpOtherParticipants.put(newPerson, personTextView);
					personTextView.setTypeface(Typeface.DEFAULT_BOLD);
					
					m_participantsView.addView(personTextView);
					m_participantsView.invalidate();
				}
			});
	}
	
	/**
	 * The function updates the participants list regarding a person that has disconnected.
	 * @param person is the disconnecting person.
	 */
	public void personDisconnected(final Person person)
	{
		m_uiHandler.post(new Runnable()
		{
			public void run()
			{
				TextView disconnectedPersonView = m_mpOtherParticipants.get(person);
				disconnectedPersonView.setPaintFlags(disconnectedPersonView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				disconnectedPersonView.setTypeface(Typeface.DEFAULT);
				disconnectedPersonView.invalidate();
			}
		});
	}
	
	/**
	 * The function updates the participants list regarding a person that has resconnected.
	 * @param person is the resconnecting person.
	 */
	public void personReconnected(final Person person)
	{
		m_uiHandler.post(new Runnable()
		{
			public void run()
			{
				TextView reconnectedPersonView = m_mpOtherParticipants.get(person);
				reconnectedPersonView.setPaintFlags(reconnectedPersonView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
				reconnectedPersonView.setTypeface(Typeface.DEFAULT_BOLD);
				reconnectedPersonView.invalidate();
			}
		});
	}

	/**
	 * The function removes a person from the participants list.
	 * @param person is the removed person.
	 */
	public void personRemoved(final Person person)
	{
		m_uiHandler.post(new Runnable()
		{
			public void run()
			{
				final TextView removedPersonView = m_mpOtherParticipants.remove(person);
				if (removedPersonView != null)
				{
					m_participantsView.removeView(removedPersonView);
					m_participantsView.invalidate();
				}
			}
		});
	}
}
